<?php

class Mis extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(array('form', 'url', 'date'));
        $this->load->library(array('session', "table", "pagination", 'form_validation'));
//validate login

        if ($this->session->userdata("username") == FALSE) {

            redirect("applicants/openLogin/You must login first");
        }

        $this->load->model("appointments_model", "", TRUE);
        $this->load->model("innovations_model", "", TRUE);
        $this->load->model("documents_model", "", TRUE);
        $this->load->model("settings/document_types_model", "", TRUE);
        $this->load->model("letters_model", "", TRUE);
        $this->load->model("debts_model", "", TRUE);
        $this->load->model("proposals_model", "", TRUE);
        $this->load->model("assets_model", "", TRUE);
        $this->load->model("bids_model", "", TRUE);
        $this->load->model("concepts_model", "", TRUE);
        $this->load->model("opis_model", "", TRUE);
        $this->load->model("trends_model", "", TRUE);
        $this->load->model("sectoral_statistics_model", "", TRUE);
        $this->load->model("partnerships_model", "", TRUE);
        $this->load->model("consolidate_model", "", TRUE);
        $this->load->model("csr_model", "", TRUE);
        $this->load->model("settings/kim_model", "", TRUE);
        $this->load->model("trainings_model", "", TRUE);
        $this->load->model("performances_model", "", TRUE);
        $this->load->model("leave_schedule_model", "", TRUE);
        $this->load->model("alumnae_model", "", TRUE);
        $this->load->model("paid_up_members_model", "", TRUE);
        $this->load->model("r_n_d_model", "", TRUE);
        $this->load->model("opportunities_model", "", TRUE);
        $this->load->model("settings/tabs_model", "", TRUE);


        $this->template = array(
            'table_open' => '<table style=\'border:5px;border-style:solid;width: 100%;\'>',
            'heading_row_start' => '<tr style=\'background-color:#ccccff;text-transform:capitalize\'>',
            'row_start' => '<tr style="background-color:#eeefff;">',
            'row_alt_start' => '<tr style="background-color:#deefff;">'
        );
        $this->table->set_template($this->template);
        $this->myConfig['uri_segment'] = 3;
        $this->myConfig['per_page'] = '50';
    }

    function index($cat, $tab=0, $message="") {
        if ($this->session->userdata("admin") != FALSE) {
            redirect("admin_mis/index/$cat/$tab/$message");
        }
    }

    function saveAppointment() {
        $this->appointments_model->newAppointment($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("appointments") . "/Appointment saved");
    }

    function saveInnovation() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = $this->upload->data();
        $data = array('upload_data' => $this->upload->data());
        $this->innovations_model->newInnovation($this->session->userdata("username"), $data['file_name']);
        redirect("mis/index/" . $this->tabs_model->getOrder("innovations") . "/Innovation saved");
    }

    function saveDocument() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = array('upload_data' => $this->upload->data());
        $this->documents_model->newDocument($this->session->userdata("username"), $data['upload_data']['file_name']);
        redirect("mis/index/" . $this->tabs_model->getOrder("documents") . "/Document saved");
    }

    function saveLetter() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = array('upload_data' => $this->upload->data());
        $this->letters_model->newLetter($this->session->userdata("username"), $data['upload_data']['file_name']);
        redirect("mis/index/" . $this->tabs_model->getOrder("letters") . "/Letter saved");
    }

    function saveLetterResponse() {
        $this->letters_model->update();
        redirect("mis/index/" . $this->tabs_model->getOrder("letters") . "/Record updated");
    }

    function saveDebt() {
        $this->debts_model->newDebt($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("debts") . "/Debt saved");
    }

    function saveProposal() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = array('upload_data' => $this->upload->data());
        $this->proposals_model->newProposal($this->session->userdata("username"), $data['upload_data']['file_name']);
        redirect("mis/index/" . $this->tabs_model->getOrder("proposals") . "/Proposal saved");
    }

    function saveAsset() {
        $this->assets_model->newAsset($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("assets") . "/Asset saved");
    }

    function saveBid() {
        $this->bids_model->newBid($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("bids") . "/Bid saved");
    }

    function saveConcept() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = array('upload_data' => $this->upload->data());
        $this->concepts_model->newConcept($this->session->userdata("username"), $data['upload_data']['file_name']);
        redirect("mis/index/" . $this->tabs_model->getOrder("concepts") . "/Concept saved");
    }

    function opis($message="") {
        $data['message'] = urldecode($message);
        $query = $this->opis_model->getAll($this->session->userdata("username"));
        $data["clients"] = $this->consolidate_model->getClients();
        $data["bus_types"] = $this->consolidate_model->getBusinessTypes();
        $this->myConfig['total_rows'] = count($query);
        $this->myConfig['base_url'] = base_url() . "index.php/mis/opis";
        $this->pagination->initialize($this->myConfig);
        $data['pagina'] = $this->pagination->create_links();
        $this->table->set_heading(array('Date Added', 'Company Name', 'Business Type', 'Year Assessed', 'Champions Trained', 'Training Year', 'Awards won', 'Relationship manager', "More Details"));
        $data['tablet'] = $this->table->generate($query);
        $this->load->view("mis/opis", $data);
    }

    function saveOpi() {
        $this->opis_model->newOpi($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("opis") . "/OPI saved");
    }

    function saveTrend() {
        $this->trends_model->newTrend($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("trends") . "/Clients saved");
    }

    function savePartnership() {
        $this->partnerships_model->newPartnership($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("partnerships") . "/Partnership saved");
    }

    function saveSectoral_statistic() {
        $this->sectoral_statistics_model->newSectoral_statistic($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("sectoral_statistics") . "/Sectoral statistic saved");
    }

    function saveCsr() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|png|jpg|gif|bmp|jpeg|xls|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = array('upload_data' => $this->upload->data());
        $this->csr_model->newCsr($this->session->userdata("username"), $data['upload_data']['file_name']);
        redirect("mis/index/" . $this->tabs_model->getOrder("csr") . "/CSR saved");
    }

    function saveTraining() {
        $this->trainings_model->newTraining($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("trainings") . "/Training saved");
    }

    function performances($message="") {
        $data['message'] = urldecode($message);
        $data["department"] = $this->consolidate_model->getDepartments();
        $query = $this->performances_model->getAll($this->session->userdata("username"));
        $temp = array();
        foreach ($query as $row) {
            unset($row['amount_paid']);
            unset($row['amount']);
            unset($row['due_date']);
            $temp[] = $row;
        }
        $query = $temp;
        $this->myConfig['total_rows'] = count($query);
        $this->myConfig['base_url'] = base_url() . "index.php/mis/performances";
        $this->pagination->initialize($this->myConfig);
        $data['pagina'] = $this->pagination->create_links();
        $this->table->set_heading(array('Date Added', 'Department', 'Annual budget', 'Quarter1', 'Quarter2', 'Quarter3', 'Quarter4', 'More Details'));
        $data['tablet'] = $this->table->generate($query);
        $this->load->view("mis/performances", $data);
    }

    function savePerformance() {
        $this->performances_model->newPerformance($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("performances") . "/Details saved");
    }

    function saveLeaveSchedule() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = array('upload_data' => $this->upload->data());
        $this->leave_schedule_model->newLeaveSchedule($this->session->userdata("username"), $data['upload_data']['file_name']);
        redirect("mis/index/" . $this->tabs_model->getOrder("leaveSchedule") . "/Leave Schedule saved");
    }

    function saveR_n_d() {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'pdf|doc|docx|xls|xlsx';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = array('upload_data' => $this->upload->data());
        $this->r_n_d_model->newR_n_d($this->session->userdata("username"), $data['upload_data']['file_name']);
        redirect("mis/index/" . $this->tabs_model->getOrder("r_n_d") . "/Record saved");
    }

    function saveOpportunities() {
        $this->opportunities_model->newOpportunities($this->session->userdata("username"));
        redirect("mis/index/" . $this->tabs_model->getOrder("opportunities") . "/Details saved");
    }

    function membership() {
        $query = $this->kim_model->getAll(FALSE);
        $data['membership_system'] = $query['membership_system'];
        $this->load->view("mis/membership", $data);
    }

    function paidUpMembers($field=NULL, $search=NULL, $offset=NULL) {
        if ($search) {
            $this->myConfig['uri_segment'] = 5;
            $this->myConfig['base_url'] = base_url() . "index.php/mis/paidUpMembers/$field/$search";
        } else {
            if ($field) {
                $offset = $field;
            } else {
                $offset = 0;
            }
            $this->myConfig['base_url'] = base_url() . "index.php/mis/paidUpMembers";
        }
        $query = $this->paid_up_members_model->adminGetAll($field, $search, $offset, $this->myConfig['per_page']);
        $countQuery = $this->paid_up_members_model->adminGetAll($field, $search);
        $this->myConfig['total_rows'] = count($countQuery);
        $this->pagination->initialize($this->myConfig);
        $data['pagina'] = $this->pagination->create_links();
        $this->table->set_heading(array('', 'Names', 'Member no', 'Grade', 'Branch', 'Town', 'Company', "More Details"));
        $data['tablet'] = $this->table->generate($query);
        $this->load->view("mis/paidUpMembers", $data);
    }

    function alumnae($field=NULL, $search=NULL, $offset=NULL) {
        if ($search) {
            $this->myConfig['uri_segment'] = 5;
            $this->myConfig['base_url'] = base_url() . "index.php/mis/alumnae/$field/$search";
        } else {
            if ($field) {
                $offset = $field;
            } else {
                $offset = 0;
            }
            $this->myConfig['base_url'] = base_url() . "index.php/mis/alumnae";
        }
        $query = $this->alumnae_model->adminGetAll($field, $search, $offset, $this->myConfig['per_page']);
        $countQuery = $this->alumnae_model->adminGetAll($field, $search);
        $this->myConfig['total_rows'] = count($countQuery);
        $this->pagination->initialize($this->myConfig);
        $data['pagina'] = $this->pagination->create_links();
        $this->table->set_heading(array('', 'Names', 'Course', 'Year', 'Telephone', 'Email', 'Employer'));
        $data['tablet'] = $this->table->generate($query);
        $this->load->view("mis/alumnae", $data);
    }

}

?>
